
Achievements = {}
Achievements.List = {}

// You have to create your achievements inside a shared file!
Achievements.Create = function(name, desc, id, func, hooktype)
	local tbl = {Name=name, Desc=desc, ID=id}
	table.insert(Achievements.List, tbl)
	if SERVER and func then
		hook.Add(hooktype,"AchievementHook_"..id,func)
	end
end

Achievements.Save = function(ply)
	if ply then
		//local name = string.Left(GAMEMODE.Name,6)
		local name = string.lower(string.gsub(GAMEMODE.Name," ","")).."_ach/"..string.sub(ply:SteamID(),11)
		local ach = ply:GetAchievements()
		if ach[1] then
			WriteToFile(name,ach)
		end
	else
		for k,ply in pairs(player.GetAll()) do
			//local name = string.Left(GAMEMODE.Name,6)
			local name = string.lower(string.gsub(GAMEMODE.Name," ","")).."_ach/"..string.sub(ply:SteamID(),11)
			local ach = ply:GetAchievements()
			if ach[1] then
				WriteToFile(name,ach)
			end
		end
	end
end

Achievements.Find = function(id)
	if type(id) == "number" then
		for k,v in pairs(Achievements.List) do
			if v.ID == id then
				return v
			end
		end
	else 
		for k,v in pairs(Achievements.List) do
			if v.Name == id then
				return v
			end
		end
	end
	return {Name="ERROR", Desc="ERROR", ID=0} // achievement does not exist
end

if SERVER then
	//function OpenModelMenu(ply,cmd,args)
	//	umsg.Start("ModelMenu",ply)
	//	umsg.End()
	//end
	//concommand.Add( "+model_menu", OpenModelMenu )
	function OpenAchMenu(ply,cmd,args)
		ply:OpenAchievementMenu()
	end
	concommand.Add( "+achievements_menu", OpenAchMenu )
end

--[[EXAMPLES

function FallHook(ply,attacker,dmg)
	if dmg:IsFallDamage() then
		ply:AddAchievement(ACH_FALL)
	end
end

function FirstBloodHook(ply,attacker,dmg)
	if attacker:IsPlayer() then
		ply:AddAchievement(ACH_FIRSTBLOOD)
	end
end

ACH_FALL = 1
ACH_FIRSTBLOOD = 2
Achievements.Create("Sidewalk Pizza","Die from falling off a high ledge.",ACH_FALL,FallHook,"DoPlayerDeath")
Achievements.Create("First Blood","Kill an enemy.",ACH_FIRSTBLOOD,FirstBloodHook,"DoPlayerDeath")]]